import qs from 'qs'

class User {
  constructor(http) {
    this.http = http;

    var token = localStorage.getItem('token');
    if(token != null)
      this.setToken(token);
  }

  setToken(token) {
    var t = window.btoa(token + ':')
    this.http.defaults.headers.common['Authorization'] = 'Basic ' + t;  
    console.log('set token = ', t, ' from', token);  
  }

  async login(data) {
    console.log(data);

    //delete axios.defaults.headers.common["Authorization"];

    var result = await this.http.post('/user/login', qs.stringify(data), {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization' : null
      }
    });

    return result;
  }

  async getOrdersForShipping(keyword, status, page) {
    var result = await this.http.get('/order/delivery?completed=' + status + '&page=' + page
      + '&keyword=' + keyword);

    return result;
  }

  async getAddresses(id) {
    return await this.http.get('/address?user_id=' + id);
  }

  async getUserById(id) {
    return await this.http.get('/user/about-me?id=' + id + '&new_flag=1');
  }

  async getUserInformation() {
    return await this.http.get('/user/about-me?new_flag=1');
  }

  async getStoreInfo(id) {
    return await this.http.get('/store/store-info?store_id=' + id);
  }

  async getOrder(id) {
    return await this.http.get('/order/view?balance_flag=0&id=' + id);
  }
}

export default User

